package com.mind.core.server;

import io.netty.channel.ChannelHandler;

import java.io.IOException;

import org.apache.log4j.Logger;

import com.mind.core.CoreErrors;
import com.mind.core.net.IoHandler;
import com.mind.core.net.processor.MessageProcessorDispatcher;
import com.mind.core.net.websocket.server.WebSocketServer;
import com.mind.core.net.websocket.server.WebSocketServerInitializer;

/**
 * Server进程,每个Server由ServerBootstrap,MessageProcessor组成
 * 
 * 
 * 
 */
public class ServerProcess  {
	private static final Logger logger = Logger.getLogger(ServerProcess.class);
	private final String name;
	private final String port;
	private final String websocketPort;
	private final String ip;
	private final ChannelHandler ioHandler;
	private final WebSocketServer webSocketServer;
	public ServerProcess(String ip, String name, String port,
			String websocketPort ,ChannelHandler ioHandler) {
		if (name == null) {
			throw new IllegalArgumentException(CoreErrors.ARG_NOT_NULL_EXCEPT);
		}
		String _ip = null;
		if (ip != null && (ip = ip.trim()).length() > 0) {
			_ip = ip;
		}
		this.ip = _ip;
		this.name = name;
		this.port = port;
		this.websocketPort = websocketPort;
		this.ioHandler = ioHandler;
		int _port = Integer.parseInt(websocketPort);
		this.webSocketServer=new WebSocketServer(ip, _port, new WebSocketServerInitializer(this.ioHandler));
	}

	/**
	 * 启动处理
	 * 
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public void start() throws IOException, InterruptedException {
		// 启动socket服务
		log("Message processor thread starting...");
		MessageProcessorDispatcher.getInstance().start();
//		if (port != null) {
//			int _port = Integer.parseInt(port);
//			EventLoopGroup bossGroup = new NioEventLoopGroup();
//			EventLoopGroup workerGroup = new NioEventLoopGroup();
//			ServerBootstrap b = new ServerBootstrap();
//			b.group(bossGroup, workerGroup)
//					.channel(NioServerSocketChannel.class)
//					.childHandler(new ChannelInitializer<SocketChannel>() {
//						@Override
//						public void initChannel(SocketChannel ch)
//								throws Exception {
//							ch.pipeline().addLast(
//									new GameDecoder(),
//									new GameEncoder(),
//									ioHandler);
//						}
//					})
//					.option(ChannelOption.TCP_NODELAY, true);
//			Channel ch = b.bind(_port).sync().channel();
//			this.bootstrap.add(b);
//			log("Listening on " + ip + ",port " + _port);
//		}
		if (websocketPort != null) {
			webSocketServer.start();
		}
	}
	/**
	 * 停止处理
	 */
	public void stop() {
		log("Message processor stoping...");
		MessageProcessorDispatcher.getInstance().stop();
		log("Message processor stopped");
		this.webSocketServer.stop();
		log("Server process stopped");
	}

	/**
	 * 获取服务的名称
	 * 
	 * @return
	 */
	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "Server process name=" + this.name + " bind address=" + this.ip
				+ " ports=" + this.port + ",websocketPort=" + websocketPort;
	}

	private void log(String msg) {
		if (logger.isInfoEnabled()) {
			logger.info("Server process [" + this.name + "]:" + msg);
		}
	}
}
